﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyUpper
{
    public partial class Form1 : Form
    {
        // 服务器
        HttpListener listener;

        // 串口
        private SerialPort serialPort;
        private string _url = "http://192.168.124.3:28087/data/";

        public Form1()
        {
            InitializeComponent();
            listener = new HttpListener();
            listener.Prefixes.Add(_url);
            try
            {
                serialPort = new SerialPort(SerialPort.GetPortNames()[0], 115200);
            }
            catch (Exception)
            {
                Console.WriteLine("找不到串口设备");
                MessageBox.Show("找不到串口设备");
                button1.Enabled = false;
                button2.Enabled = false;
                return;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            _url = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 开启服务器
            Console.WriteLine(_url);
            listener.Start();
            Console.WriteLine("Listening...");

            // 开启串口
            try
            {
                serialPort.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("串口设备开启失败");
                return;
            }

            while (true)
            {
                var context = listener.GetContext();
                var request = context.Request;
                var response = context.Response;

                if (request.HttpMethod == "POST")
                {
                    // 向串口写入字节数组
                    var buffer = new byte[request.ContentLength64];
                    request.InputStream.Read(buffer, 0, buffer.Length);
                    serialPort.Write(buffer, 0, 12);

                    // 输出字节数组
                    var data = BitConverter.ToString(buffer);
                    Console.WriteLine(data);
                }

                // 响应
                var responseString = "Data received!";
                var responseBuffer = Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = responseBuffer.Length;
                response.OutputStream.Write(responseBuffer, 0, responseBuffer.Length);
                response.OutputStream.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listener.Stop();
            serialPort.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = _url;
            label1.Left = (ClientSize.Width - label1.Width) / 2;
            textBox1.Left = (ClientSize.Width - textBox1.Width) / 2;
            button1.Left = (ClientSize.Width - button1.Width) / 2;
            button2.Left = (ClientSize.Width - button2.Width) / 2;
        }
    }
}